package com.yourname.action;

import com.yourname.custom.CustomCourseList;
import com.yourname.custom.CustomCourseStudentList;
import com.yourname.entity.Course;
import com.yourname.entity.CourseStudent;
import com.yourname.entity.Student;
import com.yourname.entity.StudentScore;
import com.yourname.store.DataStore;

import javax.xml.crypto.Data;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Scanner;

public class ScoreAction {
    private static Scanner scanner=new Scanner(System.in);

    /**
     * 录入成绩
     * @param tid 教职工号
     */
    public static void addScore(String tid){
        boolean nothingFound=true;
        /*
          根据教职工号判断该教师负责哪些课程
         */
        CustomCourseList allCourse = DataStore.getAllCourse();
        CustomCourseList mineCourses = new CustomCourseList();
        for (Course c:allCourse){
            if (c.getTid().equals(tid)){
                mineCourses.add(c);
            }
        }
        /*
            获取所有学生的选课信息，然后再从中筛选出所选课程为cid的学生
         */
        CustomCourseStudentList allCourseStudents = DataStore.getAllCourseStudent();
        CustomCourseStudentList mineCourseStudents = new CustomCourseStudentList();
        System.out.println("请输入要录入成绩的课程编号：");
        String cid = scanner.next();
        for (CourseStudent cs:allCourseStudents){
            if (cs.getCid().equals(cid)){
                mineCourseStudents.add(cs);
            }
        }
        /*
            根据筛选出的结果为这些学生依次录入成绩
         */
        for (CourseStudent cs:mineCourseStudents){
            nothingFound=false;
            String sid = cs.getSid();
            Student student = DataStore.getStudent(sid);
            System.out.println("请录入成绩记录的编号：");
            String scoreId = scanner.next();
            System.out.printf("请为学生%s(%s)录入成绩：\n",student.getName(),student.getId());
            int score = scanner.nextInt();
            BigDecimal gpa=new BigDecimal("0").setScale(2);
            boolean guake=true;//挂科标记，如果成绩小于60
            if (score>=60){
                //GPA的计算，由公式5*((x-60)/40)简化得到(x-60)/8
                gpa=new BigDecimal(score).subtract(new BigDecimal(60)).divide(new BigDecimal(8),2,RoundingMode.CEILING);
                guake=false;
            }
            StudentScore ss=new StudentScore(scoreId,sid,cs.getCid(),score,gpa,guake);
            DataStore.addScore(ss);
            DataStore.saveData();
        }
        if (nothingFound)
            System.out.println("没有找到这个课程或者没有学生选择这个课程。");
    }

    /**
     * 打印成绩单
     * @param id 学号或者教职工号
     * @param tag 登陆标识，根据登录标识生成不同类型的成绩单
     */

    public static void  print(String id,String tag){
        //如果有成绩记录（成绩记录条数大于0）则生成成绩单
        if (DataStore.getAllScoreStudent().size()>0){
            if(tag.equals("student")){
                //针对学生登陆生成的成绩单
                CustomCourseStudentList allCourseStudent = DataStore.getAllCourseStudent();
                //筛选学号为id的选课记录
                CustomCourseStudentList selectedList=new CustomCourseStudentList();
                for (CourseStudent cs:allCourseStudent){
                    if (cs.getSid().equals(id)){
                        selectedList.add(cs);
                    }

                }
                /*
                    根据选课记录打印该学生的所有成绩单
                 */
                StringBuilder printContent=new StringBuilder();
                printContent.append("学生编号,学生姓名,课程名称,成绩,GPA,是否挂科\n");
                File file=new File("score_list_"+id+".csv");
                try {
                    OutputStream os=new FileOutputStream(file);
                    for (CourseStudent cs:selectedList){
                        String cid = cs.getCid();
                        String sid = cs.getSid();
                        Course course = DataStore.getCourse(cid);
                        Student student = DataStore.getStudent(sid);
                        StudentScore ss = DataStore.getStudentScore(sid, cid);
                        printContent.append(sid).append(",");
                        printContent.append(student.getName()).append(",");
                        printContent.append(course.getName()).append(",");
                        printContent.append(ss.getScore()).append(",");
                        printContent.append(ss.getGPA()).append(",");
                        printContent.append(ss.isHanging()?"是":"").append("\n");

                    }
                    os.write(printContent.toString().getBytes());
                    os.close();
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }else{
                /*
                    针对教职工登陆生成的成绩单，筛选教职工号为id的老师负责的所有课程
                 */
                CustomCourseList allCourse = DataStore.getAllCourse();
                CustomCourseList selectedList=new CustomCourseList();
                for (Course c:allCourse){
                    if (c.getTid().equals(id)){
                        selectedList.add(c);
                    }
                }
                /*
                根据用户输入的课程编号获取所有选择该课程的学生
                 */
                System.out.println("请输入要打印的课程的成绩单（所有学生）：");
                String cid = scanner.next();
                CustomCourseStudentList allCourseStudent = DataStore.getAllCourseStudent();
                CustomCourseStudentList selectedCSList=new CustomCourseStudentList();
                for (CourseStudent cs:allCourseStudent){
                    if (cs.getCid().equals(cid)){
                        selectedCSList.add(cs);
                    }
                }
                /*
                    为每一个选择该课程的学生录入成绩
                 */
                StringBuilder printContent=new StringBuilder();
                printContent.append("学生编号,学生姓名,课程名称,成绩,GPA,是否挂科\n");
                File file=new File("score_list_"+id+".csv");
                try {
                    OutputStream os=new FileOutputStream(file);
                    for (CourseStudent cs:selectedCSList){
                        String sid = cs.getSid();
                        Course course = DataStore.getCourse(cid);
                        Student student = DataStore.getStudent(sid);
                        StudentScore ss = DataStore.getStudentScore(sid, cid);
                        printContent.append(sid).append(",");
                        printContent.append(student.getName()).append(",");
                        printContent.append(course.getName()).append(",");
                        printContent.append(ss.getScore()).append(",");
                        printContent.append(ss.getGPA()).append(",");
                        printContent.append(ss.isHanging()?"是":"").append("\n");

                    }
                    os.write(printContent.toString().getBytes());
                    os.close();
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }else{
            System.out.println("没有任何成绩记录");
        }
    }
}
